package br.edu.ufcg.p1.barzin.model;

import java.io.ByteArrayOutputStream;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

import android.graphics.Bitmap;
import br.edu.ufcg.p1.barzin.R;

import com.google.gson.annotations.SerializedName;

public class Item implements Listavel {

	private int id;

	private String nome;
	private String descricao;
	
	private double preco;
	private boolean disponivel;
	
	private Bitmap thumb;
	private Bitmap foto;
	
	public Bitmap getThumb() {
		return thumb;
	}

	public void setThumb(Bitmap thumb) {
		this.thumb = thumb;
	}
	
    public byte[] getThumbByteArray() {
    	if (this.getThumb() != null) {
    		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        	this.getThumb().compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        	return outputStream.toByteArray();
    	}
    	return null;
    }
    
	public Bitmap getFoto() {
		return foto;
	}

	public void setFoto(Bitmap foto) {
		this.foto = foto;
	}

    public byte[] getFotoByteArray() {
    	if (this.getFoto() != null) {
    		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        	this.getFoto().compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        	return outputStream.toByteArray();
    	}
    	return null;
    }

	@SerializedName("precisa_preparo")
	private boolean precisaPreparo;

	public Item(int id, String nome, String descricao,
			double preco, boolean disponivel, boolean precisaPreparo) {
		this.id = id;
		this.nome = nome;
		this.descricao = descricao;
		this.preco = preco;
		this.disponivel = disponivel;
		this.precisaPreparo = precisaPreparo;
	}

	public int getId() {
		return id;
	}

//	@Override
	public String getNome() {
		return nome;
	}

	public String getDescricao() {
		return descricao;
	}

	public double getPreco() {
		return preco;
	}

	public String getPrecoFormatado() {
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(preco).replace(".", ",");
	}
	
	public boolean isDisponivel() {
		return disponivel;
	}

	public boolean isPrecisaPreparo() {
		return precisaPreparo;
	}

	@Override
	public String toString() {
		return nome+" - R$ "+preco;
	}

//	@Override
	public int getLayout() {
		return R.layout.item_list_item;
	}

//	@Override
	public List<Listavel> getListaveis() {
		return Arrays.asList(new Listavel[]{this});
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((nome == null) ? 0 : nome.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Item other = (Item) obj;
		if (id != other.id)
			return false;
		if (nome == null) {
			if (other.nome != null)
				return false;
		} else if (!nome.equals(other.nome))
			return false;
		return true;
	}

}
